package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hxf.domain.Subject;
import com.ruoyi.hxf.domain.vo.SubjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hxf.domain.bo.AdviceBo;
import com.ruoyi.hxf.domain.vo.AdviceVo;
import com.ruoyi.hxf.domain.Advice;
import com.ruoyi.hxf.mapper.AdviceMapper;
import com.ruoyi.hxf.service.IAdviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 反馈(校长邮箱)Service业务层处理
 *
 * @author hxf
 * @date 2022-12-23
 */
@RequiredArgsConstructor
@Service
public class AdviceServiceImpl implements IAdviceService {

    private final AdviceMapper baseMapper;

    /**
     * 查询反馈(校长邮箱)
     */
    @Override
    public AdviceVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询反馈(校长邮箱)列表
     */
    @Override
    public TableDataInfo<AdviceVo> queryPageList(AdviceBo bo, PageQuery pageQuery) {
        bo.setIsDelete("0");
        LambdaQueryWrapper<Advice> lqw = buildQueryWrapper(bo);
        Page<AdviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    private LambdaQueryWrapper<Advice> buildQueryWrapper(AdviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Advice> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFeedbackTitle()), Advice::getFeedbackTitle, bo.getFeedbackTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getReplyStatus()), Advice::getReplyStatus, bo.getReplyStatus());
        lqw.eq(bo.getIsPublic() != null, Advice::getIsPublic, bo.getIsPublic());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDelete()), Advice::getIsDelete, bo.getIsDelete()).last("order by id desc");
        return lqw;
    }


    /**
     * 修改反馈(校长邮箱)
     */
    @Override
    public Boolean updateByBo(AdviceBo bo) {
        Advice update = BeanUtil.toBean(bo, Advice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Advice entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除反馈(校长邮箱)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            AdviceVo adviceVo = baseMapper.selectVoById(id);
            adviceVo.setIsDelete("1");
            Advice update = BeanUtil.toBean(adviceVo, Advice.class);
            update.setDeleteBy(String.valueOf(LoginHelper.getUserId()));
            update.setDeleteTime(DateUtils.getNowDate());
            baseMapper.updateById(update);
        }
        return true;
    }

    /**
     * 新增反馈(校长邮箱)
     */
    @Override
    public Boolean insertByBo(AdviceBo bo) {
        Advice add = BeanUtil.toBean(bo, Advice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public List<AdviceVo> SelectAllAdvice() {
        return baseMapper.SelectAllAdvice();
    }

    @Override
    public List<AdviceVo> SelectMyAdvice(Long userId) {
        return baseMapper.SelectMyAdvice(userId);
    }

    @Override
    public AdviceVo SelectAdviceById(Long id) {
        return baseMapper.SelectAdviceById(id);
    }

    @Override
    public void updateAdvice(AdviceVo adviceVo) {
        baseMapper.updateAdvice(adviceVo);
    }

    @Override
    public void updateAdviceById(Long id) {
        baseMapper.updateAdviceById(id);
    }

    @Override
    public void deleteAdviceById(Long id) {
        baseMapper.deleteAdviceById(id);
    }
}
